package de.mmis.core.base.abstracttree.serializer.internal;

import java.util.Map;

import de.mmis.core.base.DeserializableAnnotations.DeserializationMethod;
import de.mmis.core.base.abstracttree.InnerNode;
import de.mmis.core.base.abstracttree.Tree;
import de.mmis.core.base.abstracttree.serializer.AbstractSerializer;
import de.mmis.core.base.abstracttree.serializer.selector.MapSelector;

/**
 * Serializer class for (de)serializing {@link Map}s from/to Trees
 * 
 * @author Christian Koch
 */
public class MapSerializer extends AbstractSerializer<Map<?, ?>> {

	@DeserializationMethod
	public static Object deserialize(Tree[] exps, ClassLoader classLoader,
			String id, Map<String, Object> idmap) {

		return new MapSelector(new InnerNode(exps), classLoader, id, idmap);
	}

	@Override
	public Tree serializeInternal(Map<?, ?> object, boolean explicit,
			Map<Object, String> refmap, ClassLoader classLoader) {
		InnerNode ret = new InnerNode();
		for (Map.Entry<?, ?> elem : object.entrySet())
			ret.add(new InnerNode(Tree.fromObject(elem.getKey(), explicit,
					refmap, classLoader), Tree.fromObject(elem.getValue(),
					explicit, refmap, classLoader)));

		return ret;
	}

	@Override
	public String getTypeIdentifier() {
		return "map";
	}
}
